package com.shuzi.MediaShare.download;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shuzi.MediaShare.Constant;
import com.shuzi.MediaShare.R;

/**
 * 下载管理适配器
 * 
 * @author lung 下午8:34:18 2013
 */
public class DownloadMangerAdapter extends BaseAdapter
{

	private List<DownloadInfo> downloadInfos;

	private Context context;

	public DownloadMangerAdapter(Context context,
			List<DownloadInfo> downloadInfos)
	{
		this.context = context;
		this.downloadInfos = downloadInfos;
	}

	@Override
	public int getCount()
	{
		return downloadInfos.size();
	}

	@Override
	public Object getItem(int position)
	{
		return downloadInfos.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	public class ViewHolder
	{
		/** 电影名称 */
		public TextView movieName;

		/** 下载大小 */
		public TextView downloadSize;

		/** 下載速度 */
		public TextView downloadSpeed;

		/** 下載进度 */
		public TextView downloadPercent;

		/** 下载状态 */
		public TextView downloadState;

		/** 电影图片 */
		public ProgressBar downloadProgressBar;

		public ViewHolder(View baseView)
		{
			movieName = (TextView) baseView.findViewById(R.id.name);
			downloadSize = (TextView) baseView.findViewById(R.id.download_size);
			downloadSpeed = (TextView) baseView
					.findViewById(R.id.download_speed);
			downloadState = (TextView) baseView
					.findViewById(R.id.download_state);
			downloadPercent = (TextView) baseView
					.findViewById(R.id.download_download_percent_text);
			downloadProgressBar = (ProgressBar) baseView
					.findViewById(R.id.download_percent_progress);
		}

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder;
		if (convertView == null)
		{
			convertView = View.inflate(context, R.layout.download_manager_item,
					null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		refreshData(holder, downloadInfos.get(position));

		return convertView;
	}

	/**
	 * 更新数据
	 * 
	 * @param holder
	 * @param info
	 */
	public void refreshData(ViewHolder holder, DownloadInfo info)
	{
		holder.movieName.setText(info.getName());
		holder.downloadPercent.setText(info.getDownloadPercent() + "%");
		holder.downloadState.setText(Constant.downloadStates[info.getState()]);
		holder.downloadProgressBar.setProgress(info.getDownloadPercent());
		holder.downloadSpeed.setText(info.getDownloadSpeed());
		holder.downloadSize.setText(context.getResources().getString(
				R.string.download_size, info.getHadDownloadSize(),
				info.getTotleSize()));
		if (info.getState()==DownloadInfo.STATE_DOWNLOADING)
		{
			holder.downloadSpeed.setVisibility(View.VISIBLE);
		}else {
			holder.downloadSpeed.setVisibility(View.GONE);
		}
	}

}
